package com.nhh.v.controller

import com.nhh.v.anotation.ValidateHeader
import com.nhh.v.dto.RequiredRequestHeader
import com.nhh.v.service.TownService
import javax.validation.Valid
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.{PostMapping, RequestMapping, RequestParam, RestController}
import reactor.core.publisher.Mono




/**
  * Created by yuhai.cen on 2018/9/4.
  */
@RestController
@RequestMapping(path = Array("/api/town"))
@Validated
class TownController(@Autowired val townService: TownService) {

  @PostMapping(path = Array("getMyTown"))
  def getMainData(@Valid @ValidateHeader requireHeader: RequiredRequestHeader,
                  @RequestParam(value = "pindex", defaultValue = "0") pIndex: String,
                  @RequestParam(value = "psize", defaultValue = "20") pSize: String,
                  @RequestParam(value = "townId") townId: String): Mono[_] = {

    Mono.just(
      townService.getMyTown(
        Integer.valueOf(requireHeader.getUid),
        Integer.valueOf(pIndex),
        Integer.valueOf(pSize),
        Integer.valueOf(townId)
      )
    )
  }

  @PostMapping(path = Array("getMeTown"))
  def getMeTown(@Valid @ValidateHeader requireHeader: RequiredRequestHeader,
                @RequestParam(value = "townId") townId: String): Mono[_] = {
    Mono.just(townService.getMeTown(Integer.valueOf(townId)))
  }

  @PostMapping(path = Array("qrTowns"))
  def qrTowns(@Valid @ValidateHeader requireHeader: RequiredRequestHeader,
              @RequestParam(value = "pindex", defaultValue = "0") pIndex: String,
              @RequestParam(value = "psize", defaultValue = "20") pSize: String,
              @RequestParam(value = "order", defaultValue = "0") order: String): Mono[_] = {
    Mono.just(
      townService.getTowns(
        Integer.valueOf(requireHeader.getUid),
        Integer.valueOf(pIndex),
        Integer.valueOf(pSize),
        Integer.valueOf(order)
      )
    )
  }

  @PostMapping(path = Array("townCreate"))
  def townCreate(@Valid @ValidateHeader requireHeader: RequiredRequestHeader,
                 @RequestParam(value = "logo") logo: String,
                 @RequestParam(value = "townName") townName: String,
                 @RequestParam(value = "rate") rate: String,
                 @RequestParam(value = "description") description: String): Mono[_]  = {
    Mono.just(
      townService.createTown(
        Integer.valueOf(requireHeader.getUid),
        logo,
        townName,
        Integer.valueOf(rate),
        description)
    )
  }

  @PostMapping(path = Array("townUpdate"))
  def townUpdate(@Valid @ValidateHeader requireHeader: RequiredRequestHeader,
                 @RequestParam(value = "logo") logo: String,
                 @RequestParam(value = "name") townName: String,
                 @RequestParam(value = "description") description: String,
                 @Valid @RequestParam(value = "id") townId: String): Mono[_]  = {
    Mono.just(
      townService.updateTown(
        Integer.valueOf(requireHeader.getUid),
        logo,
        townName,
        description,
        Integer.valueOf(townId))
    )
  }

  @PostMapping(path = Array("townJoin"))
  def townJoin(@Valid @ValidateHeader requireHeader: RequiredRequestHeader,
                 @Valid @RequestParam(value = "townId") townId: String): Mono[_]  = {
    Mono.just(
      townService.joinTown(
        Integer.valueOf(requireHeader.getUid),
        Integer.valueOf(townId))
    )
  }

  @PostMapping(path = Array("townExit"))
  def townExit(@Valid @ValidateHeader requireHeader: RequiredRequestHeader,
               @Valid @RequestParam(value = "townId") townId: String): Mono[_]  = {
    Mono.just(
      townService.exitTown(
        Integer.valueOf(requireHeader.getUid),
        Integer.valueOf(townId))
    )
  }


}
